package com.evan.review.eventdispatch;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.evan.review.R;

/**
 * 事件分发案例演示Activity
 * 
 * 该Activity用于展示不同情况下的事件流向和消费情况：
 * 1. 只有布局消费事件
 * 2. 只有按钮消费事件
 * 3. 布局和按钮都消费事件
 */
public class EventExampleActivity extends AppCompatActivity {
    
    private static final String TAG = "EventExample";
    
    private MyFrameLayout frameLayout;
    private MyButton button;
    private TextView tvLogs;
    private RadioGroup rgLayoutConsume, rgButtonConsume;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_example);
        
        setTitle("事件分发案例演示");
        
        // 初始化视图
        initViews();
        
        // 设置点击监听器
        button.setOnClickListener(v -> {
            appendLog("Button onClick() 被调用");
            Toast.makeText(this, "按钮onClick方法消费事件", Toast.LENGTH_SHORT).show();
        });
        
        // 设置单选按钮变化监听
        rgLayoutConsume.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb_layout_consume) {
                frameLayout.setConsumeEvent(true);
                appendLog("设置：布局消费事件");
            } else {
                frameLayout.setConsumeEvent(false);
                appendLog("设置：布局不消费事件");
            }
        });
        
        rgButtonConsume.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb_button_consume) {
                button.setConsumeEvent(true);
                appendLog("设置：按钮消费事件");
            } else {
                button.setConsumeEvent(false);
                appendLog("设置：按钮不消费事件");
            }
        });
        
        // 初始化设置
        frameLayout.setConsumeEvent(false);
        button.setConsumeEvent(true);
    }
    
    private void initViews() {
        frameLayout = findViewById(R.id.my_frame_layout);
        button = findViewById(R.id.my_button);
        tvLogs = findViewById(R.id.tv_logs);
        rgLayoutConsume = findViewById(R.id.rg_layout_consume);
        rgButtonConsume = findViewById(R.id.rg_button_consume);
        
        Button btnClearLog = findViewById(R.id.btn_clear_log);
        btnClearLog.setOnClickListener(v -> {
            tvLogs.setText("");
        });
    }
    
    /**
     * 添加日志到界面
     */
    public void appendLog(String logText) {
        Log.d(TAG, logText);
        tvLogs.append(logText + "\n");
        
        // 自动滚动到底部
        int scrollAmount = tvLogs.getLayout().getLineTop(tvLogs.getLineCount()) - tvLogs.getHeight();
        if (scrollAmount > 0) {
            tvLogs.scrollTo(0, scrollAmount);
        } else {
            tvLogs.scrollTo(0, 0);
        }
    }
    
    /**
     * 获取Activity实例用于添加日志
     */
    public static EventExampleActivity getInstance(View view) {
        if (view.getContext() instanceof EventExampleActivity) {
            return (EventExampleActivity) view.getContext();
        }
        return null;
    }
    
    /**
     * 显示事件消费提示
     */
    public void showEventConsumedToast(String consumer) {
        Toast.makeText(this, consumer + "消费了事件", Toast.LENGTH_SHORT).show();
    }
} 